opendir(DH,"/Users/yaping/Documents/workspace/result/ASM/") or die;
my @normal_unjusted;
my @tumor_unjusted;
my @normal_adjusted;
my @tumor_adjusted;
my @normal_SNP;
my @tumor_SNP;
my $normal_SNP_title="track type=bedGraph name=\"normal SNP\" description=\"normal SNP\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $tumor_SNP_title="track type=bedGraph name=\"tumor SNP\" description=\"tumor SNP\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $logp_normal_unjusted_title="track type=bedGraph name=\"normal -log2 unjusted P value\" description=\"normal -log2 unjusted P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $logp_normal_adjusted_title="track type=bedGraph name=\"normal -log2 adjusted P value\" description=\"normal -log2 unjusted P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $logp_tumor_unjusted_title="track type=bedGraph name=\"tumor -log2 unjusted P value\" description=\"tumor -log2 unjusted P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
my $logp_tumor_adjusted_title="track type=bedGraph name=\"tumor -log2 adjusted P value\" description=\"tumor -log2 unjusted P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
push(@normal_unjusted,"$logp_normal_unjusted_title\n");
push(@normal_adjusted,"$logp_normal_adjusted_title\n");
push(@tumor_unjusted,"$logp_tumor_unjusted_title\n");
push(@tumor_adjusted,"$logp_tumor_adjusted_title\n");
push(@normal_SNP,"$normal_SNP_title\n");
push(@tumor_SNP,"$tumor_SNP_title\n");
foreach my $file(readdir(DH)){
	if($file=~/methylCGsRich_ASM_AllSnp_(\w+)Merge_(\w+)_pValue.txt/){
		my $sample=$1;
		my $chr=$2;
		open(FH,"</Users/yaping/Documents/workspace/result/ASM/$file");
		my @in=<FH>;
		chomp(@in);
		close(FH);
		my @logp_out;
		my @methy_out;
		my @unmethy_out;
		my $logp_title="track type=bedGraph name=\"".$sample." ".$chr." -log2 P value\" description=\"".$sample." ".$chr." -log2 P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
		my $methy_title="track type=bedGraph name=\"".$sample." ".$chr." log2 methylation fraction_to_expectation\" description=\"".$sample." ".$chr." log2 methylation fraction_to_expectation\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
		my $unmethy_title="track type=bedGraph name=\"".$sample." ".$chr." log2 unmethylation fraction_to_expectation\" description=\"".$sample." ".$chr." log2 unmethylation fraction_to_expectation\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
		
		push(@logp_out,"$logp_title\n");
		push(@methy_out,"$methy_title\n");
		push(@unmethy_out,"$unmethy_title\n");
		foreach my $line(@in){
			my @splitin = split "\t",$line;
			push(@logp_out,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[3]\n");
			push(@methy_out,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[4]\n");
			push(@unmethy_out,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[5]\n");
			if($sample eq "normal"){
				push(@normal_unjusted,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[3]\n");
			}
			else{
				push(@tumor_unjusted,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[3]\n");
			}
		}
		my $logp_out_file="methylCGsRich_ASM_AllSnp_".$sample."Merge_".$chr."_logP_unjusted.bedGraph";
		my $methy_out_file="methylCGsRich_ASM_AllSnp_".$sample."Merge_".$chr."_log2_methy.bedGraph";
		my $unmethy_out_file="methylCGsRich_ASM_AllSnp_".$sample."Merge_".$chr."_log2_unmethy.bedGraph";
		open(OUT,">/Users/yaping/Documents/workspace/result/ASM/$logp_out_file");
		print OUT @logp_out;
		close(OUT);
		open(OUT,">/Users/yaping/Documents/workspace/result/ASM/$methy_out_file");
		print OUT @methy_out;
		close(OUT);
		open(OUT,">/Users/yaping/Documents/workspace/result/ASM/$unmethy_out_file");
		print OUT @unmethy_out;
		close(OUT);
		
	}
	if($file=~/methylCGsRich_ASM_AllSnp_(\w+)Merge_(\w+)_pValue_adjusted.txt/){
		my $sample=$1;
		my $chr=$2;
		open(FH,"</Users/yaping/Documents/workspace/result/ASM/$file");
		my @in=<FH>;
		chomp(@in);
		close(FH);
		my @logp_out;
		my @methy_out;
		my @unmethy_out;
		my $logp_title="track type=bedGraph name=\"".$sample." ".$chr." -log2 adjusted P value\" description=\"".$sample." ".$chr." -log2 adjusted P value\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";	
		push(@logp_out,"$logp_title\n");
		foreach my $line(@in){
			my @splitin = split "\t",$line;
			if($splitin[5] eq "Inf"){
				push(@logp_out,"$chr\t$splitin[1]\t$splitin[1]\t10.0\n");
			}
			elsif($splitin[5] eq "NA"){
				
			}
			else{
				push(@logp_out,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[5]\n");
			}	
			if($sample eq "normal"){
				if($splitin[5] eq "Inf"){
					push(@normal_adjusted,"$chr\t$splitin[1]\t$splitin[1]\t10.0\n");
				}
				elsif($splitin[5] eq "NA"){

				}	
				else{
					push(@normal_adjusted,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[5]\n");
				}	
			}
			else{
				if($splitin[5] eq "Inf"){
					push(@tumor_adjusted,"$chr\t$splitin[1]\t$splitin[1]\t10.0\n");
				}
				elsif($splitin[5] eq "NA"){

				}
				else{
					push(@tumor_adjusted,"$chr\t$splitin[1]\t$splitin[1]\t$splitin[5]\n");
				}
			}
		}
		my $logp_out_file="methylCGsRich_ASM_AllSnp_".$sample."Merge_".$chr."_logP_adjusted.bedGraph";
		open(OUT,">/Users/yaping/Documents/workspace/result/ASM/$logp_out_file");
		print OUT @logp_out;
		close(OUT);
		
	}
	if($file=~/methylCGsRich_ASM_(\w+)Merge_(\w+)_SNP_all_afterBaseQfilter.txt/){
		my $sample=$1;
		my $chr=$2;
		open(FH,"</Users/yaping/Documents/workspace/result/ASM/$file");
		my @in=<FH>;
		chomp(@in);
		close(FH);
		my @snp_out;
		my $snp_title="track type=bedGraph name=\"".$sample." ".$chr." SNP\" description=\"".$sample." ".$chr." SNP\" visibility=full color=200,100,0 altColor=0,100,200 priority=20";
		push(@snp_out,"$snp_title\n");
		foreach my $line(@in){
			my @splitin = split "\t",$line;
			my $ratio=$splitin[2]/$splitin[1];
			my $position=abs($splitin[0]);
			push(@snp_out,"$chr\t$position\t$position\t$ratio\n");
			if($sample eq "normal"){
				push(@normal_SNP,"$chr\t$position\t$position\t$ratio\n");
			}
			else{
				push(@tumor_SNP,"$chr\t$position\t$position\t$ratio\n");
			}
		}
		my $snp_out_file="methylCGsRich_ASM_AllSnp_".$sample."Merge_".$chr."_SNP.bedGraph";
		open(OUT,">/Users/yaping/Documents/workspace/result/ASM/$snp_out_file");
		print OUT @snp_out;
		close(OUT);
	}
}

open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_normalMerge_logP_unjusted.bedGraph");
print OUT @normal_unjusted;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_tumorMerge_logP_unjusted.bedGraph");
print OUT @tumor_unjusted;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_normalMerge_logP_adjusted.bedGraph");
print OUT @normal_adjusted;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_tumorMerge_logP_adjusted.bedGraph");
print OUT @tumor_adjusted;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_tumorMerge_SNP.bedGraph");
print OUT @tumor_SNP;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_normalMerge_SNP.bedGraph");
print OUT @normal_SNP;
close(OUT);
